import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Keyboard } from '@ionic-native/keyboard';
import { ToastProvider } from '../providers/common/toastUtil';

@Component({
    templateUrl: 'app.html'
})
export class MyApp {
    // 可改为首屏动画或其他
    rootPage: any;
    platform: any = Platform;
    backButtonPressed: boolean = false;  // 用于判断返回键是否触发
    @ViewChild(Nav) nav: Nav;

    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private keyboard: Keyboard, private toastProvider: ToastProvider) {
        this.platform = platform;

        // storage.ready().then(()=>{
        //   storage.get('token').then(
        //     (token:string)=>{
        //       if(token!=null){
        //         console.log('token:'+token);
        //         this.rootPage = TabsPage;
        //       }else{
        //         console.log('token undefined');
        //         this.rootPage = LoginPage;
        //       }
        //     }
        //   );
        // });

        this.platform.ready().then(() => {
            // 初始化APP时执行
            this.initializeApp();
            // Okay, so the platform is ready and our plugins are available.
            // Here you can do any higher level native things you might need.
            this.nav.push('LoginComponent');
            statusBar.styleDefault();
            splashScreen.hide();
            // this.keyboard.disableScroll(true);
            // 推送配置
            // 运行平台监测等

            // let env = this;
            // this.nativeStorage.getItem('user')
            //     .then(function (data) {
            //         // user is previously logged and we have his data
            //         // we will let him access the app
            //         env.nav.push(UserPage);
            //         env.splashScreen.hide();
            //     }, function (error) {
            //         //we don't have the user data so we will ask him to log in
            //         env.nav.push(LoginPage);
            //         env.splashScreen.hide();
            //     });
        });
    }

    initializeApp() {
        this.platform.ready().then(() => {
            //注册返回按键事件
            this.platform.registerBackButtonAction((): any => {
                let activeVC = this.nav.getActive();
                let page = activeVC.instance;
                // 判断是否是初始页
                // if (!(page instanceof LearnTabPage)) {
                //     if (!this.nav.canGoBack()) {
                //         //当前页面为tabs，退出APP
                //         return this.showExit();
                //     }
                //     //当前页面为tabs的子页面，正常返回
                //     return this.nav.pop();
                // }
                let tabs = page.tabs;
                let activeNav = tabs.getSelected();
                if (!activeNav.canGoBack()) {
                    //当前页面为tab栏，退出APP
                    return this.showExit();
                }
                //当前页面为tab栏的子页面，正常返回
                return activeNav.pop();
            }, 101);
        });
    }

    //双击退出提示框，这里使用Ionic2的ToastController
    showExit() {
        if (this.backButtonPressed) this.platform.exitApp();  //当触发标志为true时，即2秒内双击返回按键则退出APP
        else {
            this.toastProvider.warning('再按一次退出应用', 'bottom');
            // let toast = this.toast.create({
            //     message: '再按一次退出应用',
            //     duration: 2000,
            //     position: 'bottom'
            // });
            // toast.present();
            this.backButtonPressed = true;
            //2秒内没有再次点击返回则将触发标志标记为false
            setTimeout(() => {
                this.backButtonPressed = false;
            }, 2000);
        }
    }
}

